package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"guliedu/model"
	"net/http"
	"strconv"
)

type videoInfo struct {
	ChapterID         int    `json:"chapterId"`
	CourseID          string `json:"courseId"`
	Title             string `json:"title"`
	Sort              int    `json:"sort"`
	Free              int    `json:"free"`
	VideoSourceID     string `json:"videoSourceId"`
	VideoOriginalName string `json:"videoOriginalName"`
}

type updateVideoInfo struct {
	ChapterID         int    `json:"chapter_id"`
	CourseID          string `json:"course_id"`
	Title             string `json:"title"`
	Sort              int    `json:"sort"`
	Free              int    `json:"free"`
	VideoSourceID     string `json:"video_source_id"`
	VideoOriginalName string `json:"video_original_name"`
	Duration          int    `json:"duration"`
	ID                int    `json:"id"`
	PlayCount         int    `json:"play_count"`
	Size              int    `json:"size"`
	Status            string `json:"status"`
	Version           int    `json:"version"`
}

func AddVideo(ctx *gin.Context) {
	video := &videoInfo{}
	ctx.ShouldBind(&video)

	courseID, _ := strconv.Atoi(video.CourseID)
	v := &model.EduVideo{
		CourseID:          courseID,
		ChapterID:         video.ChapterID,
		Title:             video.Title,
		VideoSourceID:     video.VideoSourceID,
		VideoOriginalName: video.VideoOriginalName,
		Sort:              video.Sort,
		PlayCount:         0,
		IsFree:            1,
		Duration:          0,
		Status:            "",
	}
	eduVideoService.Insert(v)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}

func UploadQiNiuYunVideo(ctx *gin.Context) {
	file, header, _ := ctx.Request.FormFile("file")
	url, _ := eduVideoService.UploadVideo(file, header.Size)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": url,
	})
	return
}

func SelectVideo(ctx *gin.Context) {
	id := ctx.Param("id")
	video, _ := eduVideoService.Select(id)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": video,
	})
	return
}

func UpdateVideo(ctx *gin.Context) {
	video := &updateVideoInfo{}
	ctx.ShouldBind(&video)
	fmt.Println(video)

	id := strconv.Itoa(video.ID)
	v, _ := eduVideoService.Select(id)
	v.Title = video.Title
	v.Sort = video.Sort
	v.IsFree = video.Free
	v.VideoOriginalName = video.VideoOriginalName
	v.VideoSourceID = video.VideoSourceID
	eduVideoService.Update(v)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}

func DeleteVideo(ctx *gin.Context) {
	id := ctx.Param("id")
	eduVideoService.Delete(id)
	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
	})
	return
}
